package com.ywjh.inter.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Timestamp;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ywjh.inter.entity.BloodPressureEntity;
import com.ywjh.inter.service.PatientDataService;
import com.ywjh.inter.utils.StrUtil;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class BloodPressure extends HttpServlet {
	/**
	 *
	 */
	private static final long serialVersionUID = 8896351968148857966L;

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		resp.setCharacterEncoding("UTF-8");
		resp.setContentType("text/html; charset=utf-8");
		int r = 0;
		String e = "";

		JSONObject postData = StrUtil.getPostJSON(req);
		String act = postData.getString("act");
		JSONObject data = postData.getJSONObject("data");
		JSONObject json = new JSONObject();
		switch (act) {
		case "add":
			int userId = data.getInt("user_id");
			float hi = StrUtil.getFloat(data.getString("hi"), 0.0f);
			float low = StrUtil.getFloat(data.getString("low"), 0.0f);
			String memo = data.getString("memo");
			int pulse = data.getInt("pulse");
			Timestamp time = StrUtil.getTimestampHHMISS(data.getString("time"));
			BloodPressureEntity bloodPressureEntity = new BloodPressureEntity();
			bloodPressureEntity.setCheck_time(time);
			bloodPressureEntity.setAdd_time(new Timestamp(System.currentTimeMillis()));
			bloodPressureEntity.setHi(hi);
			bloodPressureEntity.setLow(low);
			bloodPressureEntity.setPulse(pulse);
			bloodPressureEntity.setStatus(1);
			bloodPressureEntity.setUser_id(userId);
			bloodPressureEntity.setMemo(memo);
			bloodPressureEntity.setData_source("APP");
			boolean b = PatientDataService.getInstance().addBloodPressure(bloodPressureEntity);
			if (b) {
				r = 1;
			} else {
				e = "数据库错误";
			}
			break;

		case "update":
			int id = data.getInt("id");
			userId = data.getInt("user_id");
			hi = StrUtil.getFloat(data.getString("hi"), 0.0f);
			low = StrUtil.getFloat(data.getString("low"), 0.0f);
			pulse = data.getInt("pulse");
			time = StrUtil.getTimestampHHMISS(data.getString("time"));
			memo = data.getString("memo");
			bloodPressureEntity = PatientDataService.getInstance().findBloodPressureById(id);
			bloodPressureEntity.setCheck_time(time);
			bloodPressureEntity.setAdd_time(new Timestamp(System.currentTimeMillis()));
			bloodPressureEntity.setHi(hi);
			bloodPressureEntity.setLow(low);
			bloodPressureEntity.setPulse(pulse);
			bloodPressureEntity.setStatus(1);
			bloodPressureEntity.setUser_id(userId);
			bloodPressureEntity.setMemo(memo);
			bloodPressureEntity.setData_source("APP");
			b = PatientDataService.getInstance().updateBloodPressure(bloodPressureEntity);
			if (b) {
				r = 1;
			} else {
				e = "数据库错误";
			}
			break;
		case "find":
			id = data.getInt("id");
			bloodPressureEntity = PatientDataService.getInstance().findBloodPressureById(id);
			JSONObject d = new JSONObject();
			if (bloodPressureEntity != null) {
				d.put("id", bloodPressureEntity.getId());
				d.put("hi", bloodPressureEntity.getHi());
				d.put("low", bloodPressureEntity.getLow());
				d.put("pulse", bloodPressureEntity.getPulse());
				d.put("user_id", bloodPressureEntity.getUser_id());
				d.put("check_time", StrUtil.getYYYYMMDDHHMISS(bloodPressureEntity.getCheck_time()));
				d.put("add_time", StrUtil.getYYYYMMDDHHMISS(bloodPressureEntity.getAdd_time()));
				d.put("memo", bloodPressureEntity.getMemo());
				d.put("data_source", bloodPressureEntity.getData_source());
				String ds = "";
				/*if (bloodPressureEntity.getHi() <= 130 && bloodPressureEntity.getLow() <= 85) {
					ds = "ok";
				} else if (bloodPressureEntity.getHi() <= 139 && bloodPressureEntity.getLow() <= 89) {
					ds = "warning";
					// 偏高
				} else if (bloodPressureEntity.getHi() <= 159 && bloodPressureEntity.getLow() <= 99) {
					ds = "mild";
					// 轻度
				} else if (bloodPressureEntity.getHi() <= 179 && bloodPressureEntity.getLow() <= 109) {
					// 中度
					ds = "moderate";
				} else {
					// 重度
					ds = "severe";
				}*/
				if ((bloodPressureEntity.getHi() >= 90 && bloodPressureEntity.getHi() <= 140) && (bloodPressureEntity.getLow() >=60 && bloodPressureEntity.getLow() <= 90 )) {
					ds = "ok";
				} else if (bloodPressureEntity.getHi() < 90 || bloodPressureEntity.getLow() < 60) {
					ds = "warning";
					// 偏高
				} else if ((bloodPressureEntity.getHi() > 140 && bloodPressureEntity.getHi() <= 160) || (bloodPressureEntity.getLow() >90 && bloodPressureEntity.getLow() < 100 )) {
					ds = "mild";
					// 轻度
				} else if ((bloodPressureEntity.getHi() > 160 && bloodPressureEntity.getHi() <= 180) || (bloodPressureEntity.getLow() >=100 && bloodPressureEntity.getLow() <= 110 )) {
					// 中度
					ds = "moderate";
				} else {
					// 重度
					ds = "severe";
				}
				d.put("data_status", ds);
			}
			r = 1;
			json.put("d", d);
			break;
		case "data":
			userId = data.getInt("user_id");
			String startTime = data.getString("start_time");
			String endTime = data.getString("end_time");
			Map<String, List<BloodPressureEntity>> dataMap = PatientDataService.getInstance()
					.findBloodPressureByTime(startTime, endTime, userId);
			JSONArray array = new JSONArray();
			for (String timeKey : dataMap.keySet()) {
				JSONArray dataArray = new JSONArray();
				JSONObject d1 = new JSONObject();
				for (BloodPressureEntity pressureEntity : dataMap.get(timeKey)) {
					JSONObject dataJson = new JSONObject();
					dataJson.put("id", pressureEntity.getId());
					dataJson.put("hi", pressureEntity.getHi());
					dataJson.put("low", pressureEntity.getLow());
					dataJson.put("pulse", pressureEntity.getPulse());
					dataJson.put("check_time", StrUtil.getYYYYMMDDHHMISS(pressureEntity.getCheck_time()));
					dataJson.put("memo", pressureEntity.getMemo());
					dataJson.put("data_source", pressureEntity.getData_source());
					String ds = "";
					/*if (pressureEntity.getHi() <= 130 && pressureEntity.getLow() <= 85) {
						ds = "ok";
					} else if (pressureEntity.getHi() <= 139 && pressureEntity.getLow() <= 89) {
						ds = "warning";
						// 偏高
					} else if (pressureEntity.getHi() <= 159 && pressureEntity.getLow() <= 99) {
						ds = "mild";
						// 轻度
					} else if (pressureEntity.getHi() <= 179 && pressureEntity.getLow() <= 109) {
						// 中度
						ds = "moderate";
					} else {
						// 重度
						ds = "severe";
					}*/

					if ((pressureEntity.getHi() >= 90 && pressureEntity.getHi() <= 140) && (pressureEntity.getLow() >=60 && pressureEntity.getLow() <= 90 )) {
						ds = "ok";
					} else if (pressureEntity.getHi() < 90 || pressureEntity.getLow() < 60) {
						ds = "warning";
						// 偏高
					} else if ((pressureEntity.getHi() > 140 && pressureEntity.getHi() <= 160) || (pressureEntity.getLow() >90 && pressureEntity.getLow() < 100 )) {
						ds = "mild";
						// 轻度
					} else if ((pressureEntity.getHi() > 160 && pressureEntity.getHi() <= 180) || (pressureEntity.getLow() >=100 && pressureEntity.getLow() <= 110 )) {
						// 中度
						ds = "moderate";
					} else {
						// 重度
						ds = "severe";
					}
					dataJson.put("data_status", ds);
					dataArray.add(dataJson);
				}
				d1.put("query_time", timeKey);
				d1.put("array", dataArray);
				array.add(d1);
			}
			r = 1;
			json.put("d", array);
			break;
		case "overview":
			userId = data.getInt("user_id");
			startTime = data.getString("start_time");
			endTime = data.getString("end_time");
			List<BloodPressureEntity> list = PatientDataService.getInstance().listBloodPressureByTime(startTime,
					endTime, userId);
			int count = list.size();
			float highest_hi = 0;
			float highest_low = 0;
			int highest_pulse = 0;
			float lowest_hi = Float.MAX_VALUE;
			float lowest_low = Float.MAX_VALUE;
			int lowest_pulse = Integer.MAX_VALUE;
			float count_hi = 0;
			float count_low = 0;
			int count_pulse = 0;
			int ok_count = 0;
			int warning_count = 0;
			int mild_count = 0;
			int moderate_count = 0;
			int severe_count = 0;
			for (BloodPressureEntity pressureEntity : list) {
				float hi1 = pressureEntity.getHi();
				float low1 = pressureEntity.getLow();
				int pulse1 = pressureEntity.getPulse();
				count_hi += hi1;
				count_low += low1;
				count_pulse += pulse1;
				if (highest_hi < hi1) {
					highest_hi = hi1;
				}
				if (highest_low < low1) {
					highest_low = low1;
				}
				if (highest_pulse < pulse1) {
					highest_pulse = pulse1;
				}
				if (lowest_hi > hi1) {
					lowest_hi = hi1;
				}
				if (lowest_low > low1) {
					lowest_low = low1;
				}
				if (lowest_pulse > pulse1) {
					lowest_pulse = pulse1;
				}
				/*if (hi1 <= 130 && low1 <= 85) {
					ok_count++;
				} else if (hi1 <= 139 && low1 <= 89) {
					warning_count++;
					// 偏高
				} else if (hi1 <= 159 && low1 <= 99) {
					mild_count++;
					// 轻度
				} else if (hi1 <= 179 && low1 <= 109) {
					// 中度
					moderate_count++;
				} else {
					// 重度
					severe_count++;
				}*/

				if ((hi1 >= 90 && hi1 <= 140) && (low1 >=60 && low1 <= 90 )) {
					ok_count++;
				} else if (hi1 < 90 || low1 < 60) {
					warning_count++;
					// 偏高
				} else if ((hi1 > 140 && hi1 <= 160) || (low1 >90 && low1 < 100 )) {
					mild_count++;
					// 轻度
				} else if ((hi1 > 160 && hi1 <= 180) || (low1 >=100 && low1 <= 110 )) {
					// 中度
					moderate_count++;
				} else {
					// 重度
					severe_count++;
				}

			}
			float avg_hi = 0.0f;
			float avg_low=0.0f;
			float avg_pulse=0.0f;
			if (count != 0) {
				avg_hi = count_hi / count;
				avg_low = count_low / count;
				avg_pulse = count_pulse / count;
			}
			JSONObject over = new JSONObject();
			over.put("count", count);// 总数
			over.put("highest_hi", highest_hi);// 最高高压
			over.put("highest_low", highest_low);// 最高低压
			over.put("highest_pulse", highest_pulse);// 最高心率
			over.put("lowest_hi", lowest_hi);// 最低低压
			over.put("lowest_low", lowest_low);// 最低低压
			over.put("lowest_pulse", lowest_pulse);// 最低心率
			over.put("avg_hi", avg_hi);// 平均高压
			over.put("avg_low", avg_low);// 平均低压
			over.put("avg_pulse", avg_pulse);// 平均心率
			over.put("ok_count", ok_count);// 正常数
			over.put("warning_count", warning_count);// 偏高数
			over.put("mild_count", mild_count);// 轻度数
			over.put("moderate_count", moderate_count);// 中度数
			over.put("severe_count", severe_count);// 重度数
			json.put("d", over);
			r = 1;
			break;
		}

		json.put("r", r);
		json.put("e", e);
		PrintWriter pw = resp.getWriter();
		pw.write(json.toString());
		pw.flush();
		pw.close();
	}
}
